$(".tabBarBox").load("../../common-html/resource-topTab.html");
var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
var code=''
/*配置内容下拉框的展示*/
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:1,type:101
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $.each(dataInter, function (inx, data2) {
                    if (data2.name == '品类logo配置') {
                        var member = '<option selected value="'+ data2.category_id +'" >' + data2.name+ '</option>';
                        $('#category_id').append(member)
                    } else {
                        var member = '<option value="' + data2.category_id +'" >' + data2.name + '</option>';
                        $('#category_id').append(member)
                    }
                })
            }
        }
    })
})
//查询
function searchlogo() {
    $.ajax({
        url:root.sroreinter.afterSelectCategoryBrandLogoList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
        "title":$("#title").val(),
        "platform":$("#platform").val(),
        "warehouseCode":$("#warehouseCode option:selected").val(),
        "status":$("#status").val(),
        "startTime":$("#logmin").val(),
        "endTime":$("#logmax").val(),
        "categoryId":$("#category_id").val()
        }),
        success: function (data) {
            couponlist(data.data, 0);
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.platform);
            rowData.push(field.warehouseName);
            rowData.push(field.title);
            rowData.push(field.releaseTime);
            rowData.push(field.status);
            rowData.push(field.createdName);
            rowData.push(field.createdTime);
            rowData.push(field.displayContent);
            rowData.push(field.id);
            rowData.push(field.frontCategoryId);
            dataSource.push(rowData);
        });
    initDataTable(dataSource);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                this.api().column(0).nodes().each(function(cell,i){
                    cell.innerHTML = i+1;
                    //权限控制函数
                    power()
                });
            },
            "autoWidth": false,
            "order": [[ 7, "desc" ]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {"data":null,"targets":0 },
                {
                    "render": function(data,type,row,meta){
                        var val=""
                        if (row[0]==10){
                            val='签约平台'
                        }else if (row[0]=='20'){
                            val='普通平台'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[3];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function(data,type,row,meta){
                        var val=""
                        if (row[4]==10){
                            val='未生效'
                        }else if (row[4]==20){
                            val='生效'
                        }else if (row[4]==30){
                            val='失效'
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[6];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val ='<a class="powerButton 457" onclick="goNextPage(\'添加\',\'../categorylogo-html/categorylogo_copy.html?categoryssid=' + row[8] +'&displayContnts=' + encodeURI(encodeURI(row[7])) + '&frontCategoyIds=' + row[9] + '&startss=' + row[4] + '\')">复制</a>&nbsp;&nbsp;<a class="powerButton 457" onclick="goNextPage(\'编辑\',\'../categorylogo-html/categorylogo_edit.html?categoryid=' + row[8] + '&displayContent=' + encodeURI(encodeURI(row[7])) + '&frontCategoryId=' + row[9] + '\')">编辑</a>&nbsp;&nbsp;<a class="powerButton 458" onclick="deletecate('+row[8]+')">删除</a>'
                        return val;
                    },
                    "targets":9
                }
            ]
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    }
}
//跳转
function goNextPage(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//删除
function deletecate(id){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.deleteCatgoryBrandLogoAsId,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                id:id,
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        isEdit = true;
                        searchlogo();
                    },700);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        })
    });
}

//配置内容
function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");
        //通栏配置 3 4 5 10
        if(fanx.val()==2){
            window.location.href="../top-html/top.html"
        }
        if(fanx.val()==3){
            window.location.href="../resource-html/resource.html?value="+"拉幕配置"
        }
        if(fanx.val()==4){
            window.location.href="../resource-html/resource.html?value="+"浮框配置"
        }
        if(fanx.val()==5){
            window.location.href="../resource-html/resource.html?value="+"弹框配置"
        }
        //轮播图配置
        if(fanx.val()==6){
            window.location.href="../picture-html/picture.html"
        }
        //搜索栏配置
        if(fanx.val()==7){
            window.location.href="../search-html/search.html"
        }
        //品类展示区配置
        if(fanx.val()==18){
            window.location.href="../display-html/display.html"
        }
        //楼层配置
        if(fanx.val()==10){
            // window.location.href="../floor-html/floor.html"
            window.location.href = "../floorManagement-html/floorManagement.html"
        }
        if(fanx.val()==21){
            window.location.href="../categorylogo-html/categorylogo.html"
        }
        if(fanx.val()==22){
            window.location.href="../brandhalllogo-html/brandhalllogo.html"
        }
    });
}
